package com.google.sydym6.view.activities;

import com.google.sydym6.R;
import com.google.sydym6.controllers.IPlayRadioController;
import com.google.sydym6.controllers.PlayRadioController;
import com.google.sydym6.logic.IRadioScrobblerModel;
import com.google.sydym6.logic.RadioScrobblerModel;
import com.google.sydym6.logic.observers.IPlayerStateObservable;
import com.google.sydym6.logic.observers.IPlayerStateObserver;
import com.google.sydym6.logic.observers.IRecognizeStatusObservable;
import com.google.sydym6.logic.observers.IRecognizeStatusObserver;
import com.google.sydym6.view.adapters.RadioListPagerAdapter;
import com.google.sydym6.view.adapters.RadioPlayerPagerAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class RadioListActivity extends AbstractMiniPlayerActivity{

	  @Override
	  protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.radio_lists_layout);
	    RadioScrobblerModel.setContext(getApplicationContext());
	    model = RadioScrobblerModel.getInstance();
	    

	    Log.v("Multitreading", "RadioListActivity: Creating new observers");
	    IRecognizeStatusObservable radioManagerStatusObservable = (IRecognizeStatusObservable) model.getRadioManager();
		radioManagerStatusObservable.addObserver((IRecognizeStatusObserver)this);
		
		IPlayerStateObservable radioManagerStateObservable = (IPlayerStateObservable) model.getRadioManager();
		radioManagerStateObservable.addObserver((IPlayerStateObserver)this);
		
	    ImageButton playPauseButton = (ImageButton) findViewById(R.id.playPauseButton);
	    playPauseButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				System.out.println("playPauseButton was clicked");
				
				controller.playStopRadio();
			}
		});
	  }
	  
	  @Override
	  protected void onResume() {
		  super.onResume();
		  
		  pager = (ViewPager) findViewById(R.id.radioListPager);
		    pagerAdapter = new RadioListPagerAdapter(getSupportFragmentManager());
		    pager.setAdapter(pagerAdapter);

		    pager.setOnPageChangeListener(new OnPageChangeListener() {

		      @Override
		      public void onPageSelected(int position) {
		        Log.d(TAG, "onPageSelected, position = " + position);
		      }

		      @Override
		      public void onPageScrolled(int position, float positionOffset,
		          int positionOffsetPixels) {
		      }

		      @Override
		      public void onPageScrollStateChanged(int state) {
		      }
		    });
		    
		  if (model.getRadioManager().isInitialized()) {

			  LinearLayout miniPlayer = (LinearLayout) findViewById(R.id.radioMiniPlayer);
			  miniPlayer.setVisibility(View.VISIBLE);
			  
			  controller = new PlayRadioController(this);
			  
			  updatePlayerState();
			  updateRecognizeStatus();
			  setRadioName();
		  }
	  }
	  
	  @Override
	  protected void onDestroy() {
		  super.onDestroy();

		  Log.v("Multitreading", "RadioListActivity: Deleting observers");
		    IRecognizeStatusObservable radioManagerStatusObservable = (IRecognizeStatusObservable) model.getRadioManager();
			radioManagerStatusObservable.removeObserver((IRecognizeStatusObserver)this);
			
			IPlayerStateObservable radioManagerStateObservable = (IPlayerStateObservable) model.getRadioManager();
			radioManagerStateObservable.removeObserver((IPlayerStateObserver)this);
	  }
}
